package gameStream.stream
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.system.System;
	import flash.utils.Timer;
	
	import gameStream.state.BallGameObjectList;
	import gameStream.state.BallGameState;
	
	import org.gof.display.IRenderer;
	import org.gof.state.BaseGameLogic;
	import org.gof.state.BaseGameState;
	import org.gof.state.IGOStatePosition;
	import org.gof.stream.BaseGameLoop;
	import org.gof.stream.BaseGameObject;
	import org.gof.stream.IGOInputMeta;
	import org.gof.stream.ISettings;
	import org.gof.stream.ITranslator;

	public class BallGameLoop extends BaseGameLoop
	{
		private var stage:Stage;
		public function BallGameLoop(viewPort:DisplayObjectContainer, renderClass:Class, myStateManager:BaseGameLogic, translator:ITranslator,gameState:BaseGameState)
		{
			stage = viewPort.stage;
			var myRenderer:IRenderer = new renderClass(viewPort);
			super(myRenderer,myStateManager,translator,gameState);
			/*
			for (var cntx:uint = 0; cntx < viewPort.numChildren; cntx++)
			{
				var child:DisplayObject = viewPort.getChildAt(cntx);
				trace (" child: " + child.name);
			}
			*/
		}
		
		/**
		 * starts a new engine stream based on everything added
		 * 
		 * @param settings
		 */
		public override function start(settings:ISettings = null): void
		{
			super.start(settings);
			stage.addEventListener(Event.ENTER_FRAME,handleEnterFrame);
		}
		
		/**
		 * stops engine stream.
		 */
		public override function stop(): void
		{
			super.stop();
			stage.removeEventListener(Event.ENTER_FRAME,handleEnterFrame);
		}
		
		private function handleEnterFrame(e:Event): void
		{
			render();
			stateManager.incrementState();
		}
		private function render(): void
		{
			
			var objectAmount:uint = ((gameState as BallGameState).gameObjectList as BallGameObjectList).length;
			
			 
			
			for (var cntx:uint = 0; cntx <objectAmount; cntx++)
			{
				var GO: BaseGameObject = (gameState.gameObjectList as BallGameObjectList).getGOat(cntx) as BaseGameObject;
				var position:IGOStatePosition= GO.position;
				if (position && GO.displayData )
				{
						renderer.updatePosition(translator.translate(position),  cntx);
				}
				
			}
			
			renderer.render();
		}
		
	}
}